package ua.cs.stu.ics.utils;

import java.awt.*;
import java.util.ArrayList;
import java.util.StringTokenizer;

/**
 * Created by IntelliJ IDEA.
 * User: Admin
 * Date: 26.02.11
 * Time: 22:40
 * To change this template use File | Settings | File Templates.
 */
public class Util {

    public static ArrayList<Component> getAllComponents(Container c) {
        Component[] comps = c.getComponents();
        ArrayList<Component> compList = new ArrayList<Component>();
        for (Component comp : comps) {
            compList.add(comp);
            if (comp instanceof Container) {
                compList.addAll(getAllComponents((Container) comp));
            }
        }
        return compList;
    }

    public static ArrayList<String> trimToArray(String text, int maxSize){
        ArrayList<String> list = new ArrayList<String>();
        if (text.length() >maxSize){
            StringTokenizer stringTokenizer = new StringTokenizer(text);
            String buffer = "";
            while (stringTokenizer.hasMoreTokens()){
                String token = stringTokenizer.nextToken();
                if (token.length() + buffer.length()+1 > maxSize && buffer.equals(""))
                    return null;
                if (token.length() + buffer.length() > maxSize ){
                    list.add(buffer);
                    buffer = token;
                    continue;
                }
                else{
                    buffer+=" "+token;
                }
            }
            list.add(buffer);

        }
        else{
            list.add(" "+text);
        }
        return list;
    }
}
